package com.fish.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fish.model.entity.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单Mapper接口
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {
    
    /**
     * 根据用户ID查询菜单列表
     */
    @Select("SELECT DISTINCT m.* FROM menu m " +
            "INNER JOIN role_menu rm ON m.id = rm.menu_id " +
            "INNER JOIN user_role ur ON rm.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND m.is_deleted = 0 AND m.status = 1 " +
            "ORDER BY m.order_num ASC")
    List<Menu> selectMenusByUserId(@Param("userId") Long userId);
    
    /**
     * 根据角色ID查询菜单列表
     */
    @Select("SELECT m.* FROM menu m " +
            "INNER JOIN role_menu rm ON m.id = rm.menu_id " +
            "WHERE rm.role_id = #{roleId} AND m.is_deleted = 0 AND m.status = 1 " +
            "ORDER BY m.order_num ASC")
    List<Menu> selectMenusByRoleId(@Param("roleId") Long roleId);
    
    /**
     * 根据用户ID查询权限标识列表
     */
    @Select("SELECT DISTINCT m.permission FROM menu m " +
            "INNER JOIN role_menu rm ON m.id = rm.menu_id " +
            "INNER JOIN user_role ur ON rm.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND m.permission IS NOT NULL " +
            "AND m.is_deleted = 0 AND m.status = 1")
    List<String> selectPermissionsByUserId(@Param("userId") Long userId);
}